package com.mdhw.guanlin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mdhw.common.contants.CacheKeyContants;
import com.mdhw.guanlin.dao.GuanUserDao;
import com.mdhw.guanlin.entity.GuanUser;
import com.mdhw.guanlin.service.GuanUserService;
import com.mdhw.guanlin.vo.GuanUserVo;
import com.mdhw.mili.vo.user.TokenSociVo;
import com.mdhw.mili.vo.user.UserInfoVo;
import com.mdhw.user.entity.MiliUser;
import com.mdhw.user.vo.MiliUserVo;
import com.mdhw.utils.JwtUtil;
import com.mdhw.utils.RedisUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Service
public class GuanUserServiceImpl extends ServiceImpl<GuanUserDao, GuanUser> implements GuanUserService {


    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private HttpServletRequest request;

    @Override
    public GuanUserVo saveGuanUser(TokenSociVo tokenSociVo) {

        UserInfoVo userInfoVo = tokenSociVo.getUser_info();
        String phone = userInfoVo.getPhone();
        LambdaQueryWrapper<GuanUser> guLqw = new LambdaQueryWrapper<>();
        guLqw.eq(GuanUser::getPhone, phone);
        List<GuanUser> guanUserList = this.list(guLqw);
        Date now = new Date();
        if (guanUserList.size() > 0) {

            GuanUser guanUser = guanUserList.get(0);
            GuanUserVo guanUserVo = new GuanUserVo();
            BeanUtils.copyProperties(guanUser, guanUserVo);
            // 用户 已经存在

            // 生成 token
            String token = JwtUtil.getToken("Bearer", guanUserVo.getId(), "發发財",  30);
            guanUserVo.setToken(token);
            // 30 天过期
            redisUtil.set(CacheKeyContants.GUANLIN_USER_TOKEN+guanUserVo.getId(),token,60*60*24*30);
            return guanUserVo;

        } else{

            // 保存一下这个用户
            GuanUser guanUser = new GuanUser();
            BeanUtils.copyProperties(userInfoVo, guanUser);
            guanUser.setId(null);
            guanUser.setMiliUserId(Long.parseLong(userInfoVo.getId().toString()));
            guanUser.setCreateDate(now);
            guanUser.setUpdateDate(now);
            this.save(guanUser);

            GuanUserVo guanUserVo = new GuanUserVo();
            BeanUtils.copyProperties(guanUser, guanUserVo);

            // 生成 token
            String token = JwtUtil.getToken("Bearer", guanUserVo.getId(), "發发財",  30);
            guanUserVo.setToken(token);
            // 30 天过期
            redisUtil.set(CacheKeyContants.GUANLIN_USER_TOKEN+guanUserVo.getId(),token,60*60*24*30);
            return guanUserVo;
        }
    }


    @Override
    public GuanUserVo selectUserByToken() {
//        String userIdStr = WebContextUtil.getUserToken();
        String token = JwtUtil.getRequestToken(request);
        String userIdStr = JwtUtil.parseToken(token);
        if (userIdStr == null) {
            return null;
        }
        Long userId = Long.parseLong(userIdStr);
        GuanUser guanUser = this.getById(userId);
        GuanUserVo guanUserVo = new GuanUserVo();
        BeanUtils.copyProperties(guanUser,guanUserVo);
        String guanlinMiKey =CacheKeyContants.GUANLIN_MILI_USER_TOKEN+userId;
        String guanlinMiToken =(String) this.redisUtil.get(guanlinMiKey);
        guanUserVo.setMiliUserToken(guanlinMiToken);
        return guanUserVo;
    }
}
